package com.iot.controller;

import com.iot.constant.CacheKeyConstant;
import com.iot.model.dto.*;
import com.iot.model.vo.UserInfoVo;
import com.iot.service.*;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.iot.commons.ResponseResult;
import com.iot.model.entity.DeviceEntity;
import com.iot.model.query.DeviceDataQuery;
import com.iot.utils.AppPushUtils;
import com.iot.utils.ResponseResultUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/deviceData")
@RequiredArgsConstructor
public class DeviceDataController {

    private final DeviceService deviceService;

    private final UserInfoService userInfoService;

    private final DeviceDataService deviceDataService;

    private final DeviceDataBpService deviceDataBpService;

    private final DeviceDataStepsService deviceDataStepsService;

    private final DeviceDataLocationService deviceDataLocationService;

    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 分页条件搜索实现
     *
     * @return resultList
     */
    @RequestMapping(value = "/uploadBp")
    public ResponseResult uploadBp(@RequestBody DeviceBpDto dto) {
        if (null == dto) {
            return ResponseResultUtil.paramFailure();
        }
        String imei = dto.getDevice();
        if (StringUtils.isBlank(imei)) {
            return ResponseResultUtil.paramFailure();
        }
        DeviceEntity device = deviceService.getByImei(imei);
        if (null == device) {
            return ResponseResultUtil.failure("imei异常", 11013);
        }
        deviceDataBpService.upload(dto.getData(), device.getAuthId());
        return ResponseResultUtil.success();
    }

    /**
     * 分页条件搜索实现
     *
     * @return resultList
     */
    @RequestMapping(value = "/uploadStep")
    public ResponseResult uploadStep(@RequestBody DeviceStepDto dto) {
        if (null == dto) {
            return ResponseResultUtil.paramFailure();
        }
        String imei = dto.getDevice();
        if (StringUtils.isBlank(imei)) {
            return ResponseResultUtil.paramFailure();
        }
        DeviceEntity device = deviceService.getByImei(imei);
        if (null == device) {
            return ResponseResultUtil.failure("imei异常", 11013);
        }
        deviceDataStepsService.upload(dto.getData(), device.getAuthId());
        return ResponseResultUtil.success();
    }

    /**
     * 分页条件搜索实现
     *
     * @return resultList
     */
    @RequestMapping(value = "/uploadLocation")
    public ResponseResult uploadLocation(@RequestBody DeviceLocationDto dto) {
        if (null == dto) {
            return ResponseResultUtil.paramFailure();
        }
        String imei = dto.getDevice();
        if (StringUtils.isBlank(imei)) {
            return ResponseResultUtil.paramFailure();
        }
        DeviceEntity device = deviceService.getByImei(imei);
        if (null == device) {
            return ResponseResultUtil.failure("imei异常", 11013);
        }
        String cacheKey = String.format(CacheKeyConstant.DEVICE_UPLOAD_KEY, imei);
        deviceDataLocationService.upload(dto.getData(), device.getAuthId());
        LocationCacheDto cacheDto = (LocationCacheDto) redisTemplate.opsForValue().get(cacheKey);
        if (null != cacheDto) {
            UserInfoVo userInfoVo = userInfoService.getByAuthId(device.getAuthId());
            String nickName = "";
            if (null != userInfoVo) {
                nickName = userInfoVo.getNickName() == null ? userInfoVo.getPhone() : userInfoVo.getNickName();
            }
            Integer os = cacheDto.getOs();
            if (null != os && os.equals(1)) {
                AppPushUtils.sendJsonMessage(cacheDto.getCid(),cacheDto.getId(), "透传Title", nickName + "已经上传了最新的定位信息。");
            } else {
                AppPushUtils.sendMessage(cacheDto.getCid(), "透传Title", nickName + "已经上传了最新的定位信息。");
            }

            redisTemplate.delete(cacheKey);
        }
        return ResponseResultUtil.success();
    }


    /**
     * 分页条件搜索实现
     *
     * @return resultList
     */
    @RequestMapping(value = "/uploadByDevice")
    public ResponseResult uploadByDevice(@RequestBody DeviceDataQuery query) {
        return deviceDataService.uploadDeviceData(query);
    }
}
